<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <!-- 上级标准 -->
        <h2 style='font-size: 16px;font-weight: bold;'>评分标准</h2>
        <div style='background: #dfe5e5;border-radius: 6px;padding-top: 10px;'>
          <a-row>
            <a-col :span="12">
              <a-form-model-item label="标准编码" :labelCol="labelCol" :wrapperCol="wrapperCol"
                                 prop="achievementStandardCode">
                <a-input v-model="standardModel.achievementStandardCode" placeholder="请输入标准编码"></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="标准名称" :labelCol="labelCol" :wrapperCol="wrapperCol"
                                 prop="achievementStandardName">
                <a-input v-model="standardModel.achievementStandardName" placeholder="请输入标准名称"></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="标准科目" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="subjectId">
<!--                <a-input v-model="standardModel.subjectId" placeholder="请输入标准科目"  ></a-input>-->
                <j-dict-select-tag type="list" v-model="standardModel.subjectId" disabled
                                   dictCode="cou_subject cs,cs.subject_name,cs.id,cs.del_flag = 0 and cs.subject_iz_open = 1 order by cs.subject_order" placeholder="请输入标准科目" />
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="标准类型" :labelCol="labelCol" :wrapperCol="wrapperCol"
                                 prop="achievementStandardTtype">
                <j-dict-select-tag type="list" v-model="standardModel.achievementStandardTtype"
                                   dictCode="achievement_standard_ttype" placeholder="请选择评分标准类型" />
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="标准用途" :labelCol="labelCol" :wrapperCol="wrapperCol"
                                 prop="achievementStandardPurpose">
                <j-dict-select-tag type="list" v-model="standardModel.achievementStandardPurpose"
                                   dictCode="achievement_standard_purpose" placeholder="请选择标准用途" />
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="用途类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="achiStandPurposeType">
                <j-dict-select-tag type="list" v-model="standardModel.achiStandPurposeType"
                                   dictCode="achi_stand_purpose_type" placeholder="请选择标准用途类型" />
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="标准序号" :labelCol="labelCol" :wrapperCol="wrapperCol"
                                 prop="achievementStandardOrder">
                <a-input-number v-model="standardModel.achievementStandardOrder" placeholder="请输入标准序号" style="width: 100%" />
              </a-form-model-item>
            </a-col>
<!--            <a-col :span="12">-->
<!--              <a-form-model-item label="是否启用" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="izOpen">-->
<!--                <j-dict-select-tag type="list" v-model="standardModel.izOpen"-->
<!--                                   dictCode="iz_open" placeholder="请选择是否启用" />-->
<!--              </a-form-model-item>-->
<!--            </a-col>-->
          </a-row>
        </div>
        <!-- 上级标准 -->
        <!-- 明细标准 -->
        <div style='margin-top: 20px;font-size: 14px;font-weight: bold;'><a-icon type="right"/> 添加总分标准<span style='font-size: 14px;font-weight: lighter;'> （至少要有一项标准分项数据）</span></div>
        <div style='background: #eeeeee;border-radius: 6px;margin-top: 10px;padding-top: 10px;' v-for="(item, index) in orderMainModel.mainCustomerList" :key="index" >
<!--          <a-row>-->
<!--            <a-col :span="12">-->
<!--              <a-form-model-item label="上级标准" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="achievementStandardId">-->
<!--                <a-input v-model="model.achievementStandardId" placeholder="请输入所属上级标准id"  ></a-input>-->
<!--              </a-form-model-item>-->
<!--            </a-col>-->
<!--            <a-col :span="12">-->
<!--              <a-form-model-item label="科目" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="subjectId">-->
<!--                <a-input v-model="model.subjectId" placeholder="请输入科目id"  ></a-input>-->
<!--              </a-form-model-item>-->
<!--            </a-col>-->
<!--          </a-row>-->
          <a-row>
<!--            <a-col :span="12">-->
<!--              <a-form-model-item label="试卷项目Code" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="exaPapProItemCode">-->
<!--                <a-input v-model="item.exaPapProItemCode = paperId" placeholder="请输入考试项目code(来源于项目模板或项目分配表）" disabled="disabled"></a-input>-->
<!--              </a-form-model-item>-->
<!--            </a-col>-->
            <a-col :span="12">
              <a-form-model-item label="分项编码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="standItemCode">
                <a-input v-model="item.standItemCode" placeholder="请输入标准分项编码"  ></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="分项名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="standItemName">
                <a-input v-model="item.standItemName" placeholder="请输入标准分项目名称(不及格、及格、良好、优秀等）"  ></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="分项别名简称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="standItemAliasname">
                <a-input v-model="item.standItemAliasname" placeholder="请输入标准分项目别名简称"  ></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="项目序号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="standItemOrder">
                <a-input-number v-model="item.standItemOrder" placeholder="请输入项目序号" style="width: 100%" />
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="分数最低分(含)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="standItemLowestScore">
                <a-input-number v-model="item.standItemLowestScore" placeholder="请输入分数最低分(含)" style="width: 100%" />
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="分数最高分(不含)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="standItemHighestScore">
                <a-input-number v-model="item.standItemHighestScore" placeholder="请输入分数最高分(不含)" style="width: 100%" />
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="分数最低百分比(含)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="standItemLowestRate">
                <a-input-number v-model="item.standItemLowestRate" placeholder="请输入分数最低分率百分比(含)" style="width: 100%" />
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="分数最高百分比(不含)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="standItemHighestRate">
                <a-input-number v-model="item.standItemHighestRate" placeholder="请输入分数最高分率百分比(不含)" style="width: 100%" />
              </a-form-model-item>
            </a-col>
<!--            <a-col :span="12">-->
<!--              <a-form-model-item label="分项实际得分" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="standItemActualScore">-->
<!--                <a-input-number v-model="item.standItemActualScore" placeholder="请输入标准分项评分实际得分" style="width: 100%" />-->
<!--              </a-form-model-item>-->
<!--            </a-col>-->
            <a-col :span="24">
              <a-form-model-item label="" prop="" style='text-align: center;'>
                <a-button type="dashed" danger style='color: #c21111;' @click="delRowCustom(index)"><a-icon type="delete"/>删除此项</a-button>
              </a-form-model-item>
            </a-col>
          </a-row>
<!--          <a-row style='border-top: 1px solid #dbdbdb;padding-top: 10px;'>-->
<!--            <a-col :span="12">-->
<!--              <a-form-model-item label="特殊限制" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="standItSpecialRestrictions">-->
<!--                <a-input-number v-model="model.standItSpecialRestrictions" placeholder="请输入特殊限制（0无限制，1性别，2年级 3年龄，4预留）" style="width: 100%" />-->
<!--              </a-form-model-item>-->
<!--            </a-col>-->
<!--            <a-col :span="12">-->
<!--              <a-form-model-item label="特殊限制最小值" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sitemSpecRestMinvalue">-->
<!--                <a-input-number v-model="model.sitemSpecRestMinvalue" placeholder="请输入特殊限制数最小值（含即大于等于或等于）" style="width: 100%" />-->
<!--              </a-form-model-item>-->
<!--            </a-col>-->
<!--            <a-col :span="12">-->
<!--              <a-form-model-item label="特殊限制最大值" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sitemSpecRestMaxvalue">-->
<!--                <a-input-number v-model="model.sitemSpecRestMaxvalue" placeholder="请输入特殊限制数最大值（不含或小于）" style="width: 100%" />-->
<!--              </a-form-model-item>-->
<!--            </a-col>-->
<!--            &lt;!&ndash;          <a-col :span="24">&ndash;&gt;-->
<!--            &lt;!&ndash;            <a-form-model-item label="是否启用(1是启用，0否停用)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="izOpen">&ndash;&gt;-->
<!--            &lt;!&ndash;              <a-input-number v-model="model.izOpen" placeholder="请输入是否启用(1是启用，0否停用)" style="width: 100%" />&ndash;&gt;-->
<!--            &lt;!&ndash;            </a-form-model-item>&ndash;&gt;-->
<!--            &lt;!&ndash;          </a-col>&ndash;&gt;-->
<!--            &lt;!&ndash;          <a-col :span="24">&ndash;&gt;-->
<!--            &lt;!&ndash;            <a-form-model-item label="所属部门编码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sysOrgCode">&ndash;&gt;-->
<!--            &lt;!&ndash;              <a-input v-model="model.sysOrgCode" placeholder="请输入所属部门编码"  ></a-input>&ndash;&gt;-->
<!--            &lt;!&ndash;            </a-form-model-item>&ndash;&gt;-->
<!--            &lt;!&ndash;          </a-col>&ndash;&gt;-->
<!--            &lt;!&ndash;          <a-col :span="24">&ndash;&gt;-->
<!--            &lt;!&ndash;            <a-form-model-item label="0表示未删除,1表示删除" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="delFlag">&ndash;&gt;-->
<!--            &lt;!&ndash;              <a-input-number v-model="model.delFlag" placeholder="请输入0表示未删除,1表示删除" style="width: 100%" />&ndash;&gt;-->
<!--            &lt;!&ndash;            </a-form-model-item>&ndash;&gt;-->
<!--            &lt;!&ndash;          </a-col>&ndash;&gt;-->
<!--            &lt;!&ndash;          <a-col :span="24">&ndash;&gt;-->
<!--            &lt;!&ndash;            <a-form-model-item label="创建人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="createBy">&ndash;&gt;-->
<!--            &lt;!&ndash;              <a-input v-model="model.createBy" placeholder="请输入创建人"  ></a-input>&ndash;&gt;-->
<!--            &lt;!&ndash;            </a-form-model-item>&ndash;&gt;-->
<!--            &lt;!&ndash;          </a-col>&ndash;&gt;-->
<!--            &lt;!&ndash;          <a-col :span="24">&ndash;&gt;-->
<!--            &lt;!&ndash;            <a-form-model-item label="修改人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="updateBy">&ndash;&gt;-->
<!--            &lt;!&ndash;              <a-input v-model="model.updateBy" placeholder="请输入修改人"  ></a-input>&ndash;&gt;-->
<!--            &lt;!&ndash;            </a-form-model-item>&ndash;&gt;-->
<!--            &lt;!&ndash;          </a-col>&ndash;&gt;-->
<!--          </a-row>-->
        </div>
        <div @click="addRowCustom()" style='margin-top: 20px;text-align: center;'><a-button type="primary" block><a-icon type="add"/>新增总分标准项</a-button></div>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction, postAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'FormAchAchieveStandardItemForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 9 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 13 },
        },
        confirmLoading: false,
        validatorRules: {
        },
        url: {
          dynamicAdd: "/achachievementstandard/achAchievementStandard/dynamicAdd",
          edit: "/achachievestandarditem/achAchieveStandardItem/edit",
          queryById: "/achachievestandarditem/achAchieveStandardItem/queryById"
        },
        standardModel: {
          id : null,
          subjectId : null,
          achievementStandardCode : null,
          achievementStandardName : null,
          achievementStandardTtype : null,
          achievementStandardPurpose : null,
          achiStandPurposeType : null,
          achievementStandardOrder : null,
          izOpen : null,
          sysOrgCode : null,
          tenantId : null,
          delFlag : null,
          createBy : null,
          createTime : null,
          updateBy : null,
          updateTime : null,
        },
        paperId: null,
        //连续新增标准分项
        //动态增加表单
        orderMainModel: {
          mainCustomerList: [],
        },

      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add (record) {
        this.paperId = record.id
        //试卷标准默认值
        this.standardModel.subjectId = record.subjectId
        this.standardModel.achievementStandardTtype = record.achievementStandardTtype
        this.standardModel.achievementStandardPurpose = record.achievementStandardPurpose
        this.standardModel.achiStandPurposeType = record.achiStandPurposeType
        this.standardModel.izOpen = record.izOpen
        this.standardModel.sysOrgCode = record.sysOrgCode
        this.standardModel.delFlag = record.delFlag
      },
      submitForm () {
        const that = this;
        //新增-父级标准数据， 新增-标准分项明细数据
        let params = {
          achAchievementStandard : JSON.stringify(this.standardModel),
          achAchieveStandardItem : this.orderMainModel.mainCustomerList,//新增-标准分项明细数据
          paperId : this.paperId,//所选试卷
        }
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            postAction(this.url.dynamicAdd,params).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
        });
        //提交完后清空缓存动态表单数据，一面影响下次数据
        this.orderMainModel.mainCustomerList = [{}]
      },

      //连续新增标准分项
      addRowCustom () {
        let addItemData = {}
        addItemData.id = null;
        addItemData.achievementStandardId = null;
        addItemData.subjectId = this.standardModel.subjectId;
        addItemData.exaPapProItemCode = this.paperId;
        addItemData.standItemActualScore = null;
        addItemData.standItSpecialRestrictions = null;
        addItemData.sitemSpecRestMinvalue = null;
        addItemData.sitemSpecRestMaxvalue = null;
        addItemData.standItemOrder = 0;
        addItemData.standItemWeightCoefficient = null;
        addItemData.standItemFatherId = "0";
        addItemData.izOpen = 1;
        addItemData.sysOrgCode = this.standardModel.sysOrgCode;
        addItemData.delFlag = 0;
        this.orderMainModel.mainCustomerList.push(addItemData);
        this.$forceUpdate();
      },
      delRowCustom (index) {
        let all = this.orderMainModel
        all['mainCustomerList'].splice(index,1);
        this.orderMainModel.mainCustomerList.splice(index,1);
        this.$forceUpdate();
      },
    }
  }
</script>
